package com.nami.common.utils;

import com.nami.common.domain.pojo.SysUser;
import com.nami.common.exception.BizException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

public class SecurityUtils {

    /**
     * 获取用户信息
     *
     * @return security用户信息
     */
    public static SysUser getUserInfo() {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (principal instanceof SysUser) {
            return (SysUser) principal;
        }
        throw new BizException("获取用户信息失败");
    }

    /**
     * 加密
     *
     * @param password 加密的信息
     * @return 加密后的信息
     */
    public static String encryptPassword(String password) {
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        return passwordEncoder.encode(password);
    }

    public static void main(String[] args) {
        String s = encryptPassword("123456");
        System.out.println(s);
    }
}
